/*
 * Copyright 2012-2013 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.info;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeUri;

import finbudget.client.ui.table.Record;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.event.RecordChangedEvent;

public class InfoCell extends AbstractCell<Record> {

    interface Template extends SafeHtmlTemplates {
        @Template("<img title=\"{0}\"  src=\"{1}\"/>")
        SafeHtml imgWithAlt(String altText, SafeUri url);

        @Template("<div class=\"{0}\">{1}</div>")
        SafeHtml div(String className, SafeHtml text);
    }

    public interface Resources extends ClientBundle {

        interface Style extends CssResource {
            @ClassName("b-info-cell")
            public String bInfoCell();

            @ClassName("b-info-header")
            public String bInfoHeader();
        }

        @Source("InfoCell.css")
        Style style();
    }

    interface InfoCellMessages extends Messages {
    	@DefaultMessage("Запись для удаления")
    	String recordToBeDeleted();

    	@DefaultMessage("Запись изменена")
    	String recordIsChanged();
    }

    private static final Template template = GWT.create(Template.class);
    public static final Resources RESOURCES = GWT.create(Resources.class);
    private static final InfoCellMessages messages = GWT.create(InfoCellMessages.class); 

    static {
        RESOURCES.style().ensureInjected();
    }

    private final CellGrid grid;

    public InfoCell(CellGrid grid) {
        super(BrowserEvents.CLICK, BrowserEvents.KEYDOWN);
        this.grid = grid;
    }

    @Override
    public void render(Context context, Record rec, SafeHtmlBuilder sb) {
    	if (rec != null) {
	        if (rec.status != null && !rec.status.isEmpty()) {
	            sb.append(template.div(RESOURCES.style().bInfoCell(),
	                    template.imgWithAlt(rec.status, finbudget.client.resources.Resources.INSTANCE.warn16x16().getSafeUri())));
	        } else if (rec.toDelete) {
	            sb.append(template.div(RESOURCES.style().bInfoCell(),
	                    template.imgWithAlt(messages.recordToBeDeleted(), finbudget.client.resources.Resources.INSTANCE.delete16x16().getSafeUri())));
	        } else if (rec.isChanged) {
	            sb.append(template.div(RESOURCES.style().bInfoCell(),
	                    template.imgWithAlt(messages.recordIsChanged(), finbudget.client.resources.Resources.INSTANCE.info16x16().getSafeUri())));
	        }
    	}
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, Record value, NativeEvent event, ValueUpdater<Record> updater) {
        super.onBrowserEvent(context, parent, value, event, updater);
        if (BrowserEvents.CLICK.equals(event.getType())) {
            onEnterKeyDown(context, parent, value, event, updater);
        }
    }

    @Override
    protected void onEnterKeyDown(Context context, Element parent, Record value, NativeEvent event, ValueUpdater<Record> updater) {
        if (value != null) {
        	if (value.toDelete) {
        		value.toDelete = false;
        		setValue(context, parent, value);
        		grid.fireEvent(new RecordChangedEvent(value));
        	}
        }
    }

}
